package pt.iscte.pidesco.app.internal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;

import pt.iscte.pidesco.app.internal.PidescoAppActivator.ViewComponent;

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {


	private static IAction run = new Action("Teams") {
		@Override
		public void run() {
			dialog();
		}
		public String getId() {
			return "teams";
		}

		public String getText() {
			return "Teams";
		};


		public void dialog() {
			Display display = Display.getDefault();
			Shell shell = new Shell(display);
			shell.setLayout(new FillLayout());
			shell.setText("ISCDE Team - PA 2014");

			//		ScrolledComposite scrl = new ScrolledComposite(shell, SWT.BORDER | SWT.V_SCROLL);

			Composite area = new Composite(shell, SWT.NONE);
			//		scrl.setExpandVertical(true);
			//		scrl.setContent(area);

			GridLayout layout = new GridLayout(2, false);
//			layout.verticalSpacing = 20;
			area.setLayout(layout);
			area.setBackground(new Color(display, 255,255,255));

			Image image = PidescoAppActivator.getImageDescriptor("icons/iscde.png").createImage();
			new Label(area, SWT.NONE).setText("Teams");
			new Label(area, SWT.NONE).setImage(image);
			

			IExtensionRegistry reg = Platform.getExtensionRegistry();
			IExtension[] extensions = reg.getExtensionPoint(PidescoAppActivator.EXP_POINT_TEAM).getExtensions();
			
			for(IExtension ext : extensions) {
				new Label(area, SWT.NONE).setText(ext.getLabel());
				new Label(area, SWT.HORIZONTAL | SWT.SEPARATOR).setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
				
//				new Label(area, SWT.NONE).setText("main component:");
				new Label(area, SWT.NONE).setText(ext.getContributor().getName());
				new Label(area, SWT.NONE);
				
				for(IConfigurationElement member : ext.getConfigurationElements()) {
					String nameEmail = member.getAttribute("name") + ", " + member.getAttribute("user") + "@iscte-iul.pt";
					new Label(area, SWT.NONE).setText("??");
					new Label(area, SWT.NONE).setText(nameEmail);
				}
				// line break
				new Label(area, SWT.NONE);
				new Label(area, SWT.NONE);
			}
			
			shell.layout();
			shell.setSize(400, 600);
			shell.open();

			while (!shell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
//			shell.close();
		}
	};

	public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
		super(configurer);
	}

	protected void makeActions(IWorkbenchWindow window) {
		register(run);
	}

	protected void fillMenuBar(IMenuManager menuBar) {

		MenuManager viewsMenu = new MenuManager("&Views", "views");
		menuBar.add(viewsMenu);
		List<ViewComponent> components = new ArrayList<ViewComponent>(PidescoAppActivator.getInstance().getComponents());
		Collections.sort(components);

		for(final ViewComponent vc : components)
			viewsMenu.add(new Action() {
				@Override
				public String getText() {
					return vc.viewTitle;
				}

				@Override
				public String getId() {
					return vc.pluginId + "." + vc.viewId;
				}

				@Override
				public void run() {
					try {
						IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
						page.showView(PidescoAppActivator.VIEW_ID, vc.getSecondaryId(), IWorkbenchPage.VIEW_ACTIVATE);
					} catch (PartInitException e) {
						e.printStackTrace();
					}
				}

			});

		MenuManager contributionsMenu = new MenuManager("&About", "about");
		menuBar.add(contributionsMenu);
		contributionsMenu.add(run);
	}

	@Override
	protected void fillCoolBar(ICoolBarManager coolBar) {
		coolBar.add(run);
	}

}
